|
Download
FAQ History |
|
API
Search Feedback |
A Simple J2EE Application that Uses the JMS API
This section explains how to write, compile, package, deploy, and run a simple J2EE application that uses the JMS API. The application in this section uses the following components:
The section covers the following topics:
You will find the source files for this section in the directory
<INSTALL>/j2eetutorial14/examples/jms/clientmdb/. Path names in this section are relative to this directory.Writing the Application Components
This first and simplest application contains the following components:
Figure 21-1 illustrates the structure of this application.
![]()
Figure 21-1 A Simple J2EE Application: Client to Message-Driven Bean
The application client sends messages to the queue. The JMS provider--in this case, the J2EE server--delivers the messages to the message-driven bean instances, which then process the messages.
Writing the components of this application involves
Coding the Application Client: SimpleClient.java
The application client class,
src/SimpleClient.java,is almost identical to<INSTALL>/j2eetutorial14/examples/jms/simple/SimpleProducer.javain Writing the Client Programs. The only significant differences are as follows.
- You do not specify the destination name and type on the command line. Instead, the client obtains the queue name through a Java Naming and Directory Interface (JNDI) API lookup.
- You do not specify the number of messages on the command line; the number of messages is set at 3 for simplicity, and no end-of-messages message is sent.
- The JNDI lookup uses the
java:comp/env/jmsnaming context.Coding the Message-Driven Bean: MessageBean.java
The message-driven bean class,
src/MessageBean.java, implements the methodssetMessageDrivenContext,ejbCreate,onMessage, andejbRemove. TheonMessagemethod, almost identical to that of<INSTALL>/j2eetutorial14/examples/jms/simple/TextListener.javain Writing the Client Programs, casts the incoming message to aTextMessageand displays the text. The only significant difference is that it calls theMessageDrivenContext.setRollbackOnlymethod in case of an exception. This method rolls back the transaction so that the message will be redelivered.Creating and Packaging the Application
This example uses a connection factory named
jms/QueueConnectionFactoryand a queue namedjms/Queue, both of which you created in Creating the JMS Administered Objects for the Simple Examples.Creating and packaging this application involve several steps:
Compiling the Source Files and Starting the J2EE Application Server
Starting the J2EE Deploytool and Creating the Application
- Start the deploytool:
On Windows systems, choose Start
Programs
Sun Microsystems
J2EE 1.4 SDK
Deploytool.
On UNIX systems, use the
deploytoolcommand.- Choose File
New
Application EAR.
- Click Browse next to the Application File Name field and use the file chooser to locate the directory
clientmdb.- In the File Name field, type
ClientMDBApp.- Click New Application.
- Click OK.
Packaging the Application Client
To package the application client, perform the following steps:
- Choose File
New
Application Client JAR to start the Application Client Wizard.
- In the JAR File Contents screen:
- Verify that Create New AppClient Module in Application is selected and that the application is
ClientMDBApp.- In the AppClient Display Name field, type
SimpleClient.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
build/client/directory. SelectSimpleClient.classfrom the Available Files tree area and click Add, then OK.- In the General screen, select
client.SimpleClientin the Main Class combo box.- In the Message Destination References screen, click Add. In the dialog box:
- In the Resource References screen:
- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Packaging the Message-Driven Bean
To package the message-driven bean, perform the following steps:
- Choose File
New
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- Select Create New JAR Module in Application and verify that the application is
ClientMDBApp.- In the JAR Display Name field, type
MDBJAR.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
build/mdb/directory. SelectMessageBean.classfrom the Available Files tree area and click Add, then OK.- In the General screen:
- In the Configuration Options screen, check the Transaction Management box.
- In the Message-Driven Bean Settings screen:
- For the Message Listener Interface, accept the default,
javax.jms.MessageListener. (You accept this default in every example.)- Click Add in the Activation Configuration Properties area. This is where you specify the required activation configuration properties for the bean.
- Type
destinationin the Property Name field andPhysicalQueuein the Value field.- Click Add again.
- Type
destinationTypein the Property Name field andjavax.jms.Queuein the Value field.- Choose
javax.jms.Queuefrom the Destination Type menu.- Type
PhysicalQueuein the Destination Name field.- Click Deployment Settings. In the dialog box, type
jmsrain the Resource Adapter field and click OK.- In the Transaction Management screen, choose Container-Managed.
- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Updating the JNDI Names
In the J2EE Application Server, the JNDI name for a message-driven bean is the destination it receives messages from, not the bean name.
Verify that the JNDI names for the application components are correct. They should appear as shown in Table 21-1 and Table 21-2.
Table 21-1 Application Pane for ClientMDBApp Component Type Component JNDI Name EJB MessageBean jms/Queue
Table 21-2 References Pane for ClientMDBApp Ref. Type Referenced By Reference Name JNDI Name Resource SimpleClient jms/MyConnectionFactory jms/QueueConnectionFactory
Deploying the Application
Saving the Client JAR and Running the Application
- In the deploytool, select the server node.
- Select
ClientMDBAppfrom the list of deployed objects, then click Client Jar.- Choose Browse to navigate to the directory from which you will run the client. Specify the
clientmdbdirectory, click Select, then click OK. Click OK in the information dialog. You will find a file namedClientMDBAppClient.jarin the specified directory.- To run the client, use the following command:
appclient -client ClientMDBAppClient.jarThe program output in the terminal window looks something like this:
Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3The output from the message-driven bean appears in the server log file,
<J2EE_HOME>/domains/domain1/server/logs/server.log:In MessageBean.MessageBean() In MessageBean.setMessageDrivenContext() In MessageBean.ejbCreate() MESSAGE BEAN: Message received: This is message 1 MESSAGE BEAN: Message received: This is message 2 MESSAGE BEAN: Message received: This is message 3Undeploying and Removing the Application
To undeploy and remove the application, perform the following steps:
If you wish, you can manually delete the EAR and client JAR files.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.